package sysAuth

import grails.converters.JSON
import initConfig.SysInitData
import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysCommUtil
import xdTool.XdDataSource

import java.text.SimpleDateFormat

class SpecialBusinessController {
     def sysDBUtilService
    /**
     * 获取系统配置参数
     * @return
     */
    def sysViewConfig() {

        //println "response.getContentType():${response.getContentType()}"
        def result = ResultJson.resultJson()
        def cfg = SysViewCfg.first()
        result.put(ResultJson.DATA.name,cfg.getProperties())
        render result.toString()
    }

    /**
     * 获取table
     * @return
     */
    def test(){
        def db = sysDBUtilService.getDBTableColumn(XdDataSource.first(),"SYS_COLLECTION")
        def db1 = sysDBUtilService.getDBTables(XdDataSource.first())

        render db
    }


    def functionUpdate(){
        def result = ResultJson.resultJson()
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss:SSS");

        def flag = params.flag?:"update"
        def id = params?.id
        def funcNo = params.funcNo?:formatter.format(new Date());
        def path = params.path?:""
        def funcName = params.funcName?:"新增菜单"
        def icon = params.icon?:"layui-icon-list"
        def state = params.state?:"1"
        def orderNum = params.orderNum?:1
        def parent = params.parentId?SysFunctions.get(params.parentId):null
        def userNo = params.userNo?:SysInitData.SYS_ADMIN

        //缺少一个菜单本身拥有对操作权限
        def operations = params.operations?:"detail"

        try {
            if(flag=="del"){
                if(id){
                    SysFunctions.get(id).delete(flush: true)
                }
            }else{
                if(!id){
                    def a = new SysFunctions(
                            operations:operations,
                            creator:userNo,
                            updater:userNo,
                            path:path,
                            parent:parent,
                            orderNum:orderNum,
                            icon:icon,
                            state:state,
                            funcName:funcName,
                            funcNo:funcNo)
                    a.save(flush: true)
                    def aa = SysCommUtil.copyMap(a)
                    aa.put("pId",a.get("parentId"))
                    aa.put("name",a.get("funcName"))
                    aa.put("id",a.id)
                    aa.remove("parent")
                    result.put(ResultJson.DATA.name, SysCommUtil.copyMap(aa))
                }else{
                    def oldFunction = SysFunctions.get(id)
                    def existsFunc = SysFunctions.findByFuncNo(funcNo)
                    if(existsFunc && oldFunction.id != existsFunc.id){
                        result.put(ResultJson.CODE.name, ResultCode.EXISTS_ERROR.code)
                        result.put(ResultJson.MSG.name, ResultCode.EXISTS_ERROR.message)
                        render result.toString()
                        return
                    }else{
                        oldFunction.setProperty("operations",operations )
                        oldFunction.setProperty("creator",userNo )
                        oldFunction.setProperty("updater",userNo )
                        oldFunction.setProperty("path",path )
                        oldFunction.setProperty("parent",parent )
                        oldFunction.setProperty("orderNum",orderNum )
                        oldFunction.setProperty("icon",icon )
                        oldFunction.setProperty("state",state )
                        oldFunction.setProperty("funcName",funcName )
                        oldFunction.setProperty("funcNo",funcNo )
                        oldFunction.save(flush: true)
                    }
                }
            }
            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name, ResultCode.TABLE_DATA_SUCCESS.message)
        }catch (Exception e){
            e.printStackTrace()
            result.put(ResultJson.CODE.name, ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.QUERY_ERROR.message)
        }finally{
            render result.toString()
        }
    }
}
